package com.camel.system.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.model.SysRoleMenu;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import com.camel.system.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <角色菜单控制器>
 *
 * @author baily
 * @date 2019/7/5
 * @since 1.0
 **/
@RestController
@RequestMapping("/sysRoleMenu")
public class SysRoleMenuController extends BaseCommonController<SysRoleMenu> {

    @Autowired
    private SysRoleMenuService<SysRoleMenu> service;

    @Override
    public Result update(@RequestBody SysRoleMenu entity) {
        ArrayList<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        if(!StringUtils.isEmpty(entity.getMenuIds())) {
            String[] split = entity.getMenuIds().split(",");
            for (String s : split) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(Integer.parseInt(s));
                sysRoleMenu.setRoleId(entity.getRoleId());
                sysRoleMenu.setCreator(StpUtil.getLoginIdAsInt());
                sysRoleMenus.add(sysRoleMenu);
            }
            service.remove(new QueryWrapper<SysRoleMenu>().eq("role_id", entity.getRoleId()));
        }
        service.saveBatch(sysRoleMenus);
        return ResultUtil.success("操作成功");
    }

    @Override
    public BaseCommonService<SysRoleMenu> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "角色菜单关系链";
    }
}

